const Router = require('koa-router')
const { Role, Permission } = require('../models')
const { checkPermission } = require('../middleware/auth')

const router = new Router({ prefix: '/api/roles' })

// 获取角色列表
router.get('/', checkPermission('role:read'), async (ctx) => {
  try {
    const roles = await Role.findAll({
      include: [{
        model: Permission,
        as: 'permissions',
        through: { attributes: [] }
      }],
      order: [['created_at', 'ASC']]
    })

    ctx.body = {
      success: true,
      data: roles,
      total: roles.length
    }
  } catch (error) {
    console.error('Get roles error:', error)
    ctx.status = 500
    ctx.body = {
      success: false,
      message: '获取角色列表失败'
    }
  }
})

// 其他角色管理接口...
// TODO: 实现完整的角色CRUD操作

module.exports = router